# Maintainer: Tim S <stahta01@gmail.com>
# ArchLinux Contributor: Pierre-Marie de Rodat <pmderodat on #ada at freenode.net>
# ArchLinux Contributor: Rod Kay <charlie5 on #ada at freenode.net>


_realname=gprbuild-bootstrap
pkgbase=mingw-w64-${_realname}-git
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}-git
_gh_release=25.0.0
pkgver=20${_gh_release}
pkgrel=1
pkgdesc="Static GPRbuild to bootstrap XML/Ada and GPRbuild itself (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64')
url='https://github.com/AdaCore/gprbuild/'
license=('spdx:GPL-3.0-or-later with GCC-exception-3.1')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("git"
             "${MINGW_PACKAGE_PREFIX}-gcc-ada")
provides=("${MINGW_PACKAGE_PREFIX}-gprbuild-bootstrap")
conflicts=("${MINGW_PACKAGE_PREFIX}-gprbuild"
           "${MINGW_PACKAGE_PREFIX}-gprbuild-gpl"
           "${MINGW_PACKAGE_PREFIX}-gprbuild-bootstrap")
source=("https://github.com/AdaCore/gprbuild/archive/v${_gh_release}/gprbuild-${_gh_release}.tar.gz"
        "https://github.com/AdaCore/gprconfig_kb/archive/v${_gh_release}/gprconfig_kb-${_gh_release}.tar.gz"
        "https://github.com/AdaCore/xmlada/archive/v${_gh_release}/xmlada-${_gh_release}.tar.gz")
sha256sums=('d12f94c1ec0b6e219f6b162f71f57129d22426e7798092f5f85b9ec2cc818bf1'
            '802e6d38a3b110897924a9c16e143cb86360f2dde94bb5b9144c7c391e37b121'
            'dbb5984a0931311c7a787a679ef4cfaeeedd357474a585dc170140ef2251dcca')

prepare() {
  cd "${srcdir}"/gprbuild-${_gh_release}/

  # GPRbuild hard-codes references to ${MINGW_PREFIX}/libexec, but MINGW packages
  # must use ${MINGW_PREFIX}/lib instead.
  sed -i "s|libexec|lib|g" doinstall gprbuild.gpr \
    "${srcdir}"/gprconfig_kb-${_gh_release}/db/compilers.xml \
    "${srcdir}"/gprconfig_kb-${_gh_release}/db/linker.xml \
    "${srcdir}"/gprconfig_kb-${_gh_release}/db/gnat.xml

  build_date=$(date '+%Y%m%d')
  release_year=$(echo ${pkgver} | cut -d. -f1)

  # Set version information
  sed -i "s/18.0w/${pkgver}-bootstrap/" gpr/src/gpr-version.ads
  sed -i "s/19940713/${build_date}/" gpr/src/gpr-version.ads
  sed -i "s/2016/${release_year}/" gpr/src/gpr-version.ads
  sed -i "s/:= Gnatpro;/:= FSF;/" gpr/src/gpr-version.ads

  mv "${srcdir}"/gprconfig_kb-${_gh_release} "${srcdir}"/gprconfig_kb/

}

build() {
  [[ -d "${srcdir}"/build-${MSYSTEM} ]] && rm -rf "${srcdir}"/build-${MSYSTEM}
  cp -r "${srcdir}"/gprbuild-${_gh_release} "${srcdir}"/build-${MSYSTEM}
  cd "${srcdir}"/build-${MSYSTEM}

  export GNATMAKEFLAGS="-j$(nproc)"
  export DESTDIR="${srcdir}/bootstrap"
  ./bootstrap.sh \
    --prefix=${MINGW_PREFIX} \
    --libexecdir=/lib \
    --with-xmlada="${srcdir}/xmlada-${_gh_release}/"
}

package() {
  cd "$srcdir/bootstrap"
  cp -a --no-preserve=ownership -- "${srcdir}/bootstrap/${MINGW_PREFIX}" "${pkgdir}"
}
